package controllers

import (
    "git.oschina.net/hfwb/tianqi.git/tq/model"
    "fmt"
    "crypto/md5"
    "errors"
    "git.oschina.net/hfwb/tianqi.git/tq/global"
)

type Passport struct {
    base
}

func (this *Passport) Login() {
    if this.Ctx.Request.Method != "POST" {
        this.Post()
        return
    }

    username := this.GetString("username")
    password := this.GetString("password")
    staff, err := model.NewStaff().GetByName(username)
    if staff == nil {
        this.Response(errors.New("用户不存在"))
        return
    }
    if err != nil {
        this.Response(err)
        return
    }
    encodePassword := fmt.Sprintf("%x", md5.Sum([]byte(password)))
    if staff.Password != encodePassword {
        this.Response(errors.New("密码错误"))
    }
    if token, err := global.EnCodeId(staff.StaffId); err != nil {
        this.Response(err)
    } else {
        this.Response(token)
    }
}
